class FoodRecord {
  final String? foodName;
  final double? calories;
  final double? protein;
  final double? carbohydrate;
  final double? fat;
  final String? imageUrl;
  final String? recordDate;
  final int? mealType;

  const FoodRecord({
    this.foodName,
    this.calories,
    this.protein,
    this.carbohydrate,
    this.fat,
    this.imageUrl,
    this.recordDate,
    this.mealType,
  });

  static FoodRecord fromMap(Map<String, dynamic> map) {
    // 处理 mealType，可能是 int 或 String 类型
    int? mealType;
    final mealTypeValue = map['mealType'];
    if (mealTypeValue != null) {
      if (mealTypeValue is int) {
        mealType = mealTypeValue;
      } else if (mealTypeValue is String) {
        mealType = int.tryParse(mealTypeValue);
      }
    }

    return FoodRecord(
      foodName: map['foodName'] as String?,
      calories: (map['calories'] as num?)?.toDouble(),
      protein: (map['protein'] as num?)?.toDouble(),
      carbohydrate: (map['carbohydrate'] as num?)?.toDouble(),
      fat: (map['fat'] as num?)?.toDouble(),
      imageUrl: map['imageUrl'] as String?,
      recordDate: map['recordDate'] as String?,
      mealType: mealType,
    );
  }

  /// 获取用餐类型文本
  String getMealTypeText() {
    switch (mealType) {
      case 1:
        return '早餐';
      case 2:
        return '午餐';
      case 3:
        return '晚餐';
      case 4:
        return '加餐';
      default:
        return '其他';
    }
  }
}


